package com.gao.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: anshenghui
 * @create: 2021-03-16 22:23
 **/
@Configuration
public class DirectRabbitConfigOfVerifyCode {

    @Autowired
    private RabbitAdmin rabbitAdmin;

    @Bean
    public Queue verifyCodeDirectQueue() {
        return new Queue("VerifyCodeDirectQueue",true);  //true 是否持久
    }

    //Direct交换机 起名：TestDirectExchange
    @Bean
    DirectExchange verifyCodeDirectExchange() {
        return new DirectExchange("VerifyCodeDirectExchange");
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键：TestDirectRouting
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(verifyCodeDirectQueue()).to(verifyCodeDirectExchange()).with("VerifyCodeDirectRouting");
    }

    //创建初始化RabbitAdmin对象
    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        // 只有设置为 true，spring 才会加载 RabbitAdmin 这个类
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

//    @Bean
//    public void createExchangeQueue (){
//        rabbitAdmin.declareExchange(verifyCodeDirectExchange());
//        rabbitAdmin.declareQueue(verifyCodeDirectQueue());
//        rabbitAdmin.declareBinding(bindingDirect());
//    }
}
